<?php

namespace app\common\model;

/**
 * Created by PhpStorm.
 * User: openspring
 * Date: 2023/08/11
 * Time: 10:43
 */
class Tools
{
    public static function log($file, $content, $is_append = true)
    {
        if (empty($content)) {
            return;
        }
        if (is_array($content)) {
            $content = json_encode($content, 320);
        }
        $content = "[" . date('Y-m-d H:i:s') . "]" . $content . "\n";
        if ($is_append) {
            file_put_contents($file, $content, FILE_APPEND);
        } else {
            file_put_contents($file, $content);
        }
    }

    /**
     * @param $code integer
     * @param $message string
     * @param $data array|object
     * @param $url string
     * @return array
     */
    public static function returnArray(int $code = 1, string $message = '', $data = [], string $url = ''): array
    {
        return ['code' => $code, 'msg' => $message, 'data' => $data, 'url' => $url];
    }

    /**
     * @param $url string
     * @param $data string|array
     * @return array|bool|mixed|string
     */
    public static function httpsRequest(string $url, $data = [], $header = [])
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        if ($data) {
            $data_string = is_array($data) ? json_encode($data, 320) : $data;
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge([
                    'X-AjaxPro-Method:ShowList',
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($data_string)], $header)
            );
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
        }
        $request = curl_exec($curl);
        curl_close($curl);
        $tmp_arr = json_decode($request, true);
        if (is_array($tmp_arr)) {
            return $tmp_arr;
        } else {
            return $request;
        }
    }

    /**
     * 解密
     * @param string $input 密文
     * @param string $security_Key 密钥
     * @return string
     */
    public static function decrypt(string $input, string $security_Key): string
    {
        $input = base64_decode($input);
        $input = openssl_decrypt($input, 'AES-128-ECB', $security_Key);
        if (!$input) {
            return false;
        }
        return $input;
    }

    /**
     * @param $input string|array 要加密前的字符串
     * @param $security_Key string 密钥
     * @return string
     */
    public static function encrypt($input, string $security_Key): string
    {
        $input = is_array($input) ? json_encode($input, 320) : $input;
        $data = openssl_encrypt($input, 'AES-128-ECB', $security_Key);
        return base64_encode($data);
    }

    /**
     * 签名
     * @param $data array|string 解密前数据
     * @param $security_Key string 密钥
     * @return String
     */
    public static function getSign($data, string $security_Key): string
    {
        if (is_array($data)) {
            unset($data['sign']);
            ksort($data);
            $data_str = '';
            foreach ($data as $i => $k) {
                if ($k == '') {
                    continue;
                }
                $data_str .= $i . '=' . $k . '&';
            }
            $data_str .= 'app_secret=' . $security_Key;
            $data = $data_str;
        }
        return strtoupper(md5($data . $security_Key));
    }


    public static function getBankName($bank_card_no)
    {
        $host = "https://bkaear.market.alicloudapi.com";
        $path = "/bankcard/query";
        $method = "GET";
        $appcode = "04f7d93b31274dbc840cb4d6cb3898d1";
        $headers = [];
        $headers[] = "Authorization:APPCODE " . $appcode;
        $query = "card={$bank_card_no}";
        $url = $host . $path . "?" . $query;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        return json_decode(curl_exec($curl), true);
    }

    /**
     * @param array $array
     * @return array
     */
    public static function filterArray(array $array): array
    {
        foreach ($array as $key => $value) {
            if ($value === '') {
                unset($array[$key]);
            }
        }
        return $array;
    }

    public static function checkBankInfo($data)
    {
        $host = "https://zball.market.alicloudapi.com";
        $path = "/v2/bcheck";
        $method = "POST";
        $appcode = "04f7d93b31274dbc840cb4d6cb3898d1";
        $headers = array();
        $headers[] = "Authorization:APPCODE " . $appcode;
        //根据API的要求，定义相对应的Content-Type
        $headers[] = "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8";
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        return json_decode(curl_exec($curl), true);
    }

    public static function sendCode($channel_config, $sms_config, $mobile, $code)
    {
        $host = "https://wwsms.market.alicloudapi.com";
        $path = "/send_sms";
        $method = "POST";
        $headers = [];
        $headers[] = "Authorization:APPCODE " . $channel_config['app_id'];
        //根据API的要求，定义相对应的Content-Type
        $headers[] = "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8";
        $bodys = "content=code:{$code}&template_id={$sms_config['sms_content_id']}&phone_number={$mobile}";
        $url = $host . $path;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $result = curl_exec($curl);
        return json_decode($result, true);
    }

}
